# test the regexp



def get_app_name(user_agent)
  rtn = ""

  #     Mozila/_5.0_________________________________________[en]? ____________________________(......)         
  reg1 = /^([^\/[:space:]]*)(\/([^[:space:]]*))?([[:space:]]*\[[a-zA-Z][a-zA-Z]\])?[[:space:]]*\((.*)\)/

  # xxxxxxxx(xxxxxxx)
  reg2 = /^(.*)\(.*\)/

  if user_agent.match reg1
    rtn = $1 + $2
  elsif user_agent.match reg2
    rtn = $1.strip
  elsif user_agent.include? "; "
    tmp = user_agent.split("; ")
    rtn = tmp[0]
  elsif user_agent.include? ", "
    tmp = user_agent.split(", ")
    rtn = tmp[0]
  elsif user_agent.include? ", "
    tmp = user_agent.split(", ")
    rtn = tmp[0]
  elsif user_agent.include? " "
    tmp = user_agent.split(" ")
    rtn = tmp[0]
  else
    rtn = user_agent
  end

  if rtn.include? "/"
    temps = rtn.split "/"
    name = temps[0]
    version = temps[1]
    if temps.size == 2 and (version.match /[[0-9]+.]?[0-9]+/)
      rtn = name
    end
  end

  rtn
end

begin
  test1 = "Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; SPH-M910 Build/ECLAIR) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17"
  test2 = "Android-GData-Contacts/1.2 (SPH-M910 ECLAIR); gzip"
  test3 = "Android/2.1-update1; Samsung SPH-M910; 1.1.1"
  test4 = "TwitterAndroid/1.0.5 (109) SPH-M910/7 (Samsung;SPH-M910)"
  test5 = "Pandora/1.5.3 Android/2.1-update1 SPH-M910"
  test6 = "CORE/6.506.4.1 OpenCORE/2.02 (Linux;Android 2.1-update1)"
  test7 = "YahooMobileMail/1.0 (Android Mail; 1.0.4) (SPH-M910;Samsung;SPH-M910;2.1-update1/ECLAIR)"
  test8 = "Android/2.2.1; Samsung SPH-M910; 1.1.1"
  test9 = "Rdio Android v0.8.5, Device SPH-M910, Build 2.1-update1"
  
  puts get_app_name(test1)
  puts get_app_name(test2)
  puts get_app_name(test3)
  puts get_app_name(test4)
  puts get_app_name(test5)
  puts get_app_name(test6)
  puts get_app_name(test7)
  puts get_app_name(test8)
  puts get_app_name(test9)

rescue Exception => e
  puts e
end
